package objects;

import java.util.ArrayList;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Circle;

import globals.FreeVector;
import states.SlickGame;

public class WaveSource {
	int x,y,r, rateWaveSpawn, curTime;
	float rateWaveSpread;
	Color colorMe,colorWave;
	ArrayList<Wave> waveList;
	FreeVector direction;
	int speed=5;
	
	public WaveSource(int xin, int yin, int rin, int rateSpawn, float rateSpread, Color me, Color wave){
		x=xin;
		y=yin;
		r=rin;
		rateWaveSpawn=rateSpawn;
		rateWaveSpread=rateSpread;
		colorMe=me;
		colorWave=wave;
		curTime=0;
		waveList=new ArrayList<Wave>();
		speed=(int)(5+(3*Math.random()));
		direction=new FreeVector(speed,(float)((4*Math.PI)*Math.random()));
	}
	public void update(int delta){
		curTime += delta;
		if(curTime>rateWaveSpawn){
			curTime=0;
			waveList.add(new CircleWave(x, y, rateWaveSpread, colorWave));
		}
		
		for(int i=0; i<waveList.size();i++){
			waveList.get(i).update();
			if(waveList.get(i).getRadius()>80){
				waveList.remove(i);
				i--;
			}
		}
		
		if(x<0){direction.add(new FreeVector(speed,0));}
		else if(x>SlickGame.width){direction.add(new FreeVector(speed,(float)(Math.PI)));}
		
		if(y<0){direction.add(new FreeVector(speed,(float)(Math.PI/2)));}
		else if(y>SlickGame.height){direction.add(new FreeVector(speed,(float)((3*Math.PI)/2)));}
		
		x+=direction.getX();
		y+=direction.getY();
	}
	public void draw(Graphics g){
		if(!waveList.isEmpty()){
			for(int i=0; i<waveList.size();i++){
				waveList.get(i).draw(g);
			}
		}
		g.setColor(colorMe);
		g.draw(new Circle(x,y,r));
	}
}
